package com.yhy.manager.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yhy.domain.UserInfo;
import com.yhy.domain.mongo.Comment;
import com.yhy.domain.mongo.Publish;
import com.yhy.domain.mongo.Video;
import com.yhy.domain.vo.CommentVo;
import com.yhy.domain.vo.Movements;
import com.yhy.domain.vo.PageResult;
import com.yhy.domain.vo.VideoVo;
import com.yhy.dubbo.api.UserApi;
import com.yhy.dubbo.api.UserInfoApi;
import com.yhy.dubbo.api.mongo.CommentApi;
import com.yhy.dubbo.api.mongo.PublishApi;
import com.yhy.dubbo.api.mongo.VideoApi;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ManagerService {

    @Reference
    private UserInfoApi userInfoApi;

    @Reference
    private UserApi userApi;

    @Reference
    private VideoApi videoApi;

    @Reference
    private PublishApi publishApi;

    @Reference
    private CommentApi commentApi;


    //用户数据翻页
    public ResponseEntity userPage(Integer page, Integer pagesize) {
        //mybatisplus查询mysql分页
        IPage<UserInfo> iPage = userInfoApi.findAll(page,pagesize);
        //构造PageResult
        PageResult pageResult = new PageResult(page,pagesize, (int) iPage.getTotal(),iPage.getRecords());

        return ResponseEntity.ok(pageResult);
    }


    //用户基本信息
    public ResponseEntity userDetail(Long userId) {
        UserInfo userInfo = userInfoApi.findById(userId);
        return ResponseEntity.ok(userInfo);
    }

    //视频记录翻页
    public ResponseEntity findVideosByUid(Integer page, Integer pagesize, Long uid) {
//        1.调用api查询出PageResult<Video>
        PageResult pageResult = videoApi.findAll(page,pagesize,uid);

        List<Video> items = (List<Video>) pageResult.getItems();
//        2.封装videoVo
        List<VideoVo> list = new ArrayList<>();
        for (Video item : items) {
            UserInfo userInfo = userInfoApi.findById(item.getUserId());
            VideoVo vo = VideoVo.init(userInfo, item);
            list.add(vo);
        }
        pageResult.setItems(list);
        return ResponseEntity.ok(pageResult);
    }

    //查询动态
    public ResponseEntity findAllMovements(Integer page, Integer pagesize, Long uid, Integer state) {
//        1.调用api查询PageResult<Publish>
        PageResult pageResult = publishApi.findAll(page,pagesize,uid,state);
        List<Publish> items = (List<Publish>) pageResult.getItems();
//        2.封装pageResult<Movements>
        List<Movements> list = new ArrayList<>();
        for (Publish item : items) {
            UserInfo userInfo = userInfoApi.findById(item.getUserId());
            Movements vo = Movements.init(userInfo, item);
            list.add(vo);
        }
        pageResult.setItems(list);
        return ResponseEntity.ok(pageResult);
    }

    //根据id查询动态详情
    public ResponseEntity publishDetail(String publishId) {
        Publish publish = publishApi.findById(publishId);
        UserInfo userInfo = userInfoApi.findById(publish.getUserId());
        Movements vo = Movements.init(userInfo, publish);
        return ResponseEntity.ok(vo);
    }

    //查询评论列表
    public ResponseEntity findAllComments(Integer page, Integer pagesize, String messageId) {
//        1.调用api查询PageResult<Comment>
        PageResult pageResult = commentApi.queryCommentList(page, pagesize, messageId);
        List<Comment> items = (List<Comment>) pageResult.getItems();
//        2.封装对应vo对象 commentVo
        List<CommentVo> list = new ArrayList<>();
        for (Comment item : items) {
            UserInfo userInfo = userInfoApi.findById(item.getUserId());
            CommentVo vo = CommentVo.init(userInfo, item);
            list.add(vo);
        }
        pageResult.setItems(list);
        return ResponseEntity.ok(pageResult);
    }
}
